package com.bsj.pay.service.impl;

import com.bsj.pay.config.GlobalConfig;
import com.bsj.pay.dao.ConfigDao;
import com.bsj.pay.model.ConfigBean;
import com.bsj.pay.service.ConfigService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigDao dao;

    @Override
    public long insert(ConfigBean t) {
        // TODO Auto-generated method stub
        return dao.insert(t);
    }

    @Override
    public int deleteById(Long id) {
        // TODO Auto-generated method stub
        return dao.deleteById(id);
    }

    @Override
    public List<ConfigBean> queryList(ConfigBean t) {
        // TODO Auto-generated method stub
        return dao.queryList(t);
    }

    @Override
    public int update(ConfigBean t) {
        // TODO Auto-generated method stub
        return dao.update(t);
    }

    @Override
    public ConfigBean getById(Long Id) {
        // TODO Auto-generated method stub
        return dao.getById(Id);
    }

    @Override
    public long count(ConfigBean t) {
        // TODO Auto-generated method stub
        return dao.count(t);
    }

    @Override
    public PageInfo<ConfigBean> pageList(ConfigBean t, int pageNum, int pageSize) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int removeById(Long id) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<ConfigBean> queryAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getValueByKey(String key) {
        // TODO Auto-generated method stub
        return dao.getValueByKey(key);
    }

    @Override
    public void init() {
        List<ConfigBean> configList = dao.queryAll();
        for (ConfigBean cf : configList) {
            try {
                if ("sitename".equals(cf.getKey())) GlobalConfig.sitename = cf.getValue();
                else if ("proxy_url".equals(cf.getKey())) GlobalConfig.proxy_url = cf.getValue();
                else if ("operator".equals(cf.getKey())) GlobalConfig.operator = Integer.parseInt(cf.getValue());
                else if ("pay_time_out".equals(cf.getKey()))
                    GlobalConfig.pay_time_out = Integer.parseInt(cf.getValue());
                else if ("query_time_out".equals(cf.getKey()))
                    GlobalConfig.query_time_out = Integer.parseInt(cf.getValue());
                else if ("max_arrears_amount".equals(cf.getKey()))
                    GlobalConfig.max_arrears_amount = Integer.parseInt(cf.getValue());
                else if ("notify_type".equals(cf.getKey())) GlobalConfig.notify_type = Integer.parseInt(cf.getValue());
                else if ("order_account_type".equals(cf.getKey()))
                    GlobalConfig.order_account_type = Integer.parseInt(cf.getValue());
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                continue;
            }
        }
        GlobalConfig.isInit = true;
    }
}
